/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal_pappalardo_sacchi.bean;

import it.polimi.meteocal_pappalardo_sacchi.entity.WeatherCondition;
import java.io.Serializable;
import javax.ejb.Stateless;
import net.aksingh.java.api.owm.AbstractWeatherData.Weather;
import net.aksingh.java.api.owm.DailyForecastData.Forecast;


/**
 *
 * @author Elena
 */
@Stateless
public class WeatherManager implements Serializable {

    public WeatherCondition createFromForecast(Forecast forecast) {

        WeatherCondition weatherCond = new WeatherCondition();
        //In Celsius 
        weatherCond.setTemperature(forecast.getTemperature_Object().getDayTemperature());
        //Wind speed in m/s
        weatherCond.setWindStrength(forecast.getWindSpeed());
        //set weather condition
        convertWeatherName(forecast.getWeather_List().get(0), weatherCond);
        
        return weatherCond;
    }

    /**
     * 
     * @param event weather condition of the event
     * @param forecast weather condition of the forecast
     * @return true if the forecast is unsuitable for the event
     */
    public boolean checkBadCondition(WeatherCondition event, WeatherCondition forecast){
        if (checkBadTemperature(event.getTemperature(), forecast.getTemperature()))
            return true;
        if (checkBadWind(event.getWindStrength(), forecast.getWindStrength()))
            return true;
        if (checkBadSky(event.isCloud(), forecast.isCloud()))
            return true;
        if (checkBadSky(event.isRain(), forecast.isRain()))
            return true;
        if (checkBadSky(event.isSnow(), forecast.isSnow()))
            return true;
        return false;
    }
    
    /**
     * 
     * @param tEvent
     * @param tForecast
     * @return true if the temperature is unsuitable for the event
     */
    public boolean checkBadTemperature(float tEvent, float tForecast){
        if(tForecast < tEvent)
            return true;
        return false;
    }
    
    /**
     * 
     * @param wEvent
     * @param wForecast
     * @return true if the wind speed is unsuitable for the event
     */
    public boolean checkBadWind(float wEvent, float wForecast){
        if(wForecast > wEvent)
            return true;
        return false;
    }
    
    /**
     * general method to check precipitation
     * @param eventSky 
     * @param forecastSky
     * @return true if the forecast precipitation is unsuitable for the event
     */
    public boolean checkBadSky(boolean eventSky, boolean forecastSky){
        if((eventSky==true) &&(eventSky==forecastSky))
            return true;
        return false;
    }
    
    private void convertWeatherName(Weather weather, WeatherCondition weatherCond) {
        String weatherName = weather.getWeatherName();
        switch(weatherName){
            case "Clouds":
                weatherCond.setCloud(true);
                return;
            case "Rain":
                weatherCond.setRain(true);
                return;
            case "Snow":
                weatherCond.setSnow(true);
                return;
            default:
                return;
        }
    }
}
